package mobweb.booktrack;

import java.io.Serializable;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Book implements Serializable{
    

    /**
     * Notice the id field will be -1 if the book is not in the sqlite database 
     */
    private static final long serialVersionUID = 1L;
    public final static int READ = 0, WISH_TO_READ = 1, READING = 2, UNSPECIFIED = 3;
    private String bookName;
    private String description;
    private String author;
    private String category;
    private String ISBN;
    private int flag;
    private long id;
    /**
     * 
     * @param bookName
     * @param description
     * @param author
     * @param category
     * @param iSBN
     * @param flag
     * @param id -1 if the book is not in the DB, its id in it otherwise
     */
    public Book(String bookName, String description, String author,
	    String category, String iSBN, int flag, long id) {
	this.bookName = bookName;
	this.description = description;
	this.author = author;
	this.category = category;
	ISBN = iSBN;
	this.flag = flag;
	this.id = id;
    }
    
    public Book() {
    	this.bookName = null;
    	this.description = null;
    	this.author = null;
    	this.category = null;
    	ISBN = null;
    	this.flag = UNSPECIFIED;
    	this.id = -1;
    }
    
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String toString() {
	return ("[Book]["+id+"]["+bookName+"]["+description+"]["+ISBN+"]["+flag+"]["+author+"]["+category+"]");
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public String getBookName() {
	return this.bookName;
    }

    public void setBookName(String bookName) {
	this.bookName = bookName;
    }
    
    public String getShortDescription() {
	if (description == null)
	    return null;
	else if (description.length()>120)
	    return (this.description.substring(0, 120)+"[...]");
	else
	    return description;
    }

    public String getDescription() {
	return this.description;
    }

    public void setDescription(String description) {
	this.description = description;
    }

    public String getISBN() {
	return this.ISBN;
    }

    public void setISBN(String iSBN) {
	this.ISBN = iSBN;
    }
    
    public boolean writeToDB(Context c) {
		SQLiteHelper sqlite_helper = new SQLiteHelper(c);
		SQLiteDatabase sqldb = sqlite_helper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put("bookName", this.getBookName());
		values.put("description", this.getDescription());
		values.put("ISBN", this.getISBN());
		values.put("author", this.getAuthor());
		values.put("flag", this.getFlag());
		values.put("category", this.getCategory());
		
		// lest check if book already exists in database
		Cursor cursor = sqldb.query("booksRead", new String[] { "_id" }, "_id = " +  id, null, null, null, null,
				null);

		boolean exists = (cursor.getCount() > 0);
		cursor.close();
		
		if (exists) {
			// Update rows
			int intSuccess = sqldb.update("booksRead", values, "_id = " + id, null);
			sqldb.close();
			
			if (intSuccess != 0){
			    return true;
			}
			else return false;
		} else {
			// Insert rows
			long success = sqldb.insert("booksRead", null, values);
			sqldb.close();
		
			if (success!=-1){
			    id=success;
			    return true;
			}
			else return false;
		}
    }
    
    public boolean deleteFromDB(Context c){
	SQLiteHelper sqlite_helper = new SQLiteHelper(c);
	SQLiteDatabase sqldb = sqlite_helper.getWritableDatabase();
	
	int i = sqldb.delete("booksRead", "_id = "+id, null);
	sqldb.close();
	
	if (i!=1) return false;
	else return true;
    }
}
